# @Author   : 懒羊羊
# @File     : wk_log.py
# @Time     : 2024/12/23 18:01
import json

import pymysql
import requests

from apis.wk_login import Login
from common.config import pts_path, url


class Log:
    token = ""

    def __init__(self, t):
        self.token = t
        self.headers = {'Admin-Token': self.token}
        self.dbc = pymysql.connect(user="root", password="123456", database="crm9", port=3306, host="192.168.88.100")

    def add_File(self, furl=None, f1data=None, f2data=None):
        if furl == None:
            furl = f"{url}/file/upload"
        if f1data == None:
            f1data = {"type": "img"}
        if f2data == None:
            f2data = {"file": (
                "ll.png", open(r"D:\python-workspace\python-lizhe\python-base-trl\apiAuto\pts\ll.png", "rb"),
                "image/pn")}

        f_r = requests.post(furl, data=f1data, files=f2data, headers=self.headers)
        bid = f_r.json().get("batchId")
        return f_r

    def get_bid(self):
        bid = self.add_File().json().get("batchId")
        return bid

    def add_log(self, add_url, add_data):
        # if add_url == None:
        #     add_url = f"{url}/OaLog/addOrUpdate"
        # if add_data == None:
        #     add_data = {"categoryId": "1", "content": "今日", "tomorrow": "明日", "question": "问题", "batchId": bid}

        l_r = requests.post(add_url, json=add_data, headers=self.headers)
        return l_r

    def cx_log(self, cx_url=None, cx_data=None):
        if cx_url == None:
            cx_url = f"{url}/OaLog/queryList"
        if cx_data == None:
            cx_data = {"page": "1", "limit": "5", "by": "1"}
        cx_r = requests.post(cx_url, data=cx_data, headers=self.headers)
        return cx_r

    def get_alllogid(self):
        cx_r = self.cx_log()
        # 将json格式的字符串转成字典
        data = json.loads(cx_r.text)
        # 通过列表推导式获取所有的logid
        log_ids = [item["logId"] for item in data["data"]["list"]]
        print(log_ids)
        return log_ids

    def get_logid(self):
        cx_r = self.cx_log()
        # 将json格式的字符串转成字典
        data = json.loads(cx_r.text)
        # 通过列表推导式获取所有的logid
        log_ids = [item["logId"] for item in data["data"]["list"]]
        log_id = log_ids[0]
        print(log_id)
        return log_id

    def update_log(self, update_url, up_data):

        # update_url = f"{url}/OaLog/addOrUpdate"
        #
        # up_data = {
        #     "logId": logId, "categoryId": "1", "content": "这里是今日内容的修改~~",
        #     "tomorrow": "这里明日工作计划的修改~",
        #     "question": "这里是今日遇到的问题的修改"}
        update_r = requests.post(update_url, json=up_data, headers=self.headers)
        return update_r

    def del_log(self, del_url, del_data):
        # if del_url == None:
        #     del_url = f"{url}/OaLog/deleteById"
        # if del_data == None:
        #     del_data = {"logId": log_id}
        del_r = requests.post(del_url, data=del_data, headers=self.headers)
        return del_r

    def del_alllog(self, del_url=None, sql=None):
        if del_url == None:
            del_url = f"{url}/OaLog/deleteById"
        if sql == None:
            sql = "SELECT log_id from 72crm_oa_log where create_user_id =(SELECT user_id from 72crm_admin_user where username='admin');"

        runner = self.dbc.cursor()
        # 执行sql语句
        runner.execute(sql)
        log_id = runner.fetchall()
        for i in log_id:
            print(i)
            del_data = {"logId": i}
            del_r = requests.post(del_url, data=del_data, headers=self.headers)
            text = del_r.json().get("code")
            print(text)


if __name__ == '__main__':
    log = Log(Login().get_token())
    # print(log)
    # r = log.add_File(furl="http://192.168.88.100:8080/file/upload", f1data='{"type": "img"}')
    # print(r.text)
    # 删除日志
    # r = log.del_log()
    # 查询日志
    # r = log.get_alllogid()
    r = log.get_logid()
